package it.polimi.jita.cp.block;

import java.util.List;

import org.apache.commons.cli2.CommandLine;
import org.apache.commons.cli2.Option;
import org.apache.commons.cli2.option.Command;

public abstract class InputBuilder<T extends Input> {
	private Command command;
	private CommandLine cl;

	public Input createInput(CommandLine cl) throws Exception {
		this.cl = cl;
		Input input = createInput();

		// Files paths for CSV reports
		input.setNeedToSaveCSVFiles(cl.hasOption("-o"));
		if (input.isNeedToSaveCSVFiles()) {
			@SuppressWarnings("unchecked")
			List<String> filePaths = cl.getValues("-o");
			// TODO Check what happens when no arguments are specified...
			for (String fp : filePaths) {
				input.addCsvFilePath(fp);
			}

			input.setWithHeaders(cl.hasOption("-h"));
		}

		input.setNeedToSaveGraphs(cl.hasOption("-oc"));
		if (input.isNeedToSaveCharts()) {
			@SuppressWarnings("unchecked")
			List<String> filePaths = cl.getValues("-oc");
			for (String fp : filePaths) {
				input.addChartFilePath(fp);
			}
		}

		input.setNeedToShowGraphs(cl.hasOption("-sc"));
		return input;
	}

	protected abstract T createInput() throws Exception;

	public void setCommand(Command command) {
		this.command = command;
	}

	protected String getOptionValue(String optName) {
		Option o;
		if (command != null)
			o = command.getChildren().findOption(optName);
		else
			o = cl.getOption(optName);
		return (String) cl.getValue(o);
	}

	protected List<?> getOptionValues(String optName) {
		Option o = command.getChildren().findOption(optName);
		return cl.getValues(o);
	}

	protected CommandLine getCl() {
		return cl;
	}

}